/*
 *  SDLVideo.cpp
 *  breemlib
 *
 *  Created by James on 8/4/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "SDLVideo.h"
#include "Filters.h"
#include "hq2x.h"

int screenWidth = 800;
int screenHeight = 600;

CSDLVideo::CSDLVideo()
{
//for hq2x
//	InitLUTs();

	Screen = 0;
}

int CSDLVideo::Init(int w,int h,int b)
{
	int flags = SDL_SWSURFACE;

	if(SDL_InitSubSystem(SDL_INIT_VIDEO) == -1) {
		return(1);
	}

	screenW = width = w;
	screenH = height = h;
	bpp = b;
	Screen = new unsigned long[w * (h + 16)];

	if(bpp != 0)
		flags |= SDL_FULLSCREEN;

	message("video initialized, machine screen = %dx%d, bpp = %d\n",w,h,b);
	surface = SDL_SetVideoMode(screenWidth,screenHeight,bpp,flags);
	if(surface == NULL) {
		fprintf(stderr, "Unable to set %dx%d video: %s\n",width,height,SDL_GetError());
		return(1);
	}

	return(0);
}

void CSDLVideo::Kill()
{
	if(Screen)
		delete[] Screen;
	Screen = 0;
}

void CSDLVideo::SetSize(int w,int h)
{
	if(Screen)
		delete[] Screen;
	width = w;
	height = h;
	Screen = new unsigned long[w * (h + 16)];
}

void CSDLVideo::StartBlit()
{

}

void CSDLVideo::StopBlit()
{
	unsigned long *dest = (unsigned long*)surface->pixels;
	int pitch = surface->pitch / 4;
	
	dest += (screenW / 2) - width;
	dest += ((screenH / 2) - height) * pitch;

	Television2x(Screen,dest,width,height,pitch);
	SDL_Flip(surface);
}

void *CSDLVideo::GetSurface()
{
	return(Screen);
}

int CSDLVideo::GetPitch()
{
	return(width * 4);
}
